package com.example.StudentComponent;


import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.example.pojo.Course;
import com.example.pojo.ParseStudent;
import com.example.pojo.Student;
import org.apache.commons.codec.digest.MurmurHash3;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class SelectCourse extends Box {

    JTable Table;
    Vector<String> course = new Vector<>();
    Vector<Vector<String>> courses = new Vector<Vector<String>>();
    TableModel tm;


    //构造方法
    public SelectCourse(String user) {
        super(BoxLayout.Y_AXIS);
        //组装
        JPanel jPanel = new JPanel();
        jPanel.setBackground(new Color(208, 210, 213, 179));
        jPanel.setMaximumSize(new Dimension(700, 50));
        JButton b1=new JButton("添加");
        JButton b3=new JButton("查询");
        JLabel name=new JLabel("课程名");
        JTextField t1=new JTextField(6);
        jPanel.add(name);
        jPanel.add(t1);
        jPanel.add(b3);
        jPanel.add(Box.createHorizontalStrut(50));
        jPanel.add(b1);
        this.add(jPanel);



        //组装表格
        String []ss={"课程号","课程名","教师","学时","时期","上课时间","上课教室","简介"};
        for (int i = 0; i < ss.length; i++) {
            course.add(ss[i]);
        }
        requestData("http://localhost:8080/course","Post");
        tm=new DefaultTableModel(courses,course);
        Table=new JTable(tm){
            @Override
            public boolean isCellEditable(int row,int column){
                return false;
            }
        };

        //只能选中一行
        Table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();//单元格渲染器
        tcr.setHorizontalAlignment(JLabel.CENTER);//居中显示
        Table.setDefaultRenderer(Object.class, tcr);//设置渲染器
        JScrollPane scrollPane=new JScrollPane(Table);
        Table.setFont(new Font("宋体",Font.PLAIN,15));
        this.add(scrollPane);



        /*
         * 添加按钮
         * */
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Vector<ParseStudent> parseStudents = requestGetData("http://localhost:8080/select?id=" + user);
                int row = Table.getSelectedRow();//获取行
                DefaultTableModel dm= (DefaultTableModel) tm;
                String sid=user;
                String cid=(String) tm.getValueAt(row, 0);
                String courseName=(String) tm.getValueAt(row, 1);
                String name=parseStudents.get(0).getName();

               int f=0;
                for (int i = 0; i < parseStudents.size(); i++) {
                    if(courseName.equals(parseStudents.get(i).getCourse())){
                        JOptionPane.showMessageDialog(null,"课程已添加,请勿重复添加" );
                        f=1;
                        break;
                    }
                }

               if(f==0){
                   HttpUtil.sendGet("http://localhost:8080/student/"+sid+"/"+cid+"/"+courseName+"/"+name);
                   JOptionPane.showMessageDialog(null,"添加成功" );
               }
            }
        });

        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!t1.getText().equals("")) {
                    requestData("http://localhost:8080/selectCourse?name=" + t1.getText(), "Get");
                }else   requestData("http://localhost:8080/course","Post");
              Table.updateUI();
            }
        });





    }

    //请求数据
    public  void requestData( String url,String mon){
        String result=null;
       if (mon.equals("Post")){
          result=HttpUtil.sendPost(url);
       }else {
           result=HttpUtil.sendGet(url);
       }
        Vector<Course> course= ResultUtil.ResultVectorCourse(result);
        Vector<Vector<String>> ss=new Vector<>();
        for (int i = 0; i < course.size(); i++) {
           if(course.get(i).getState().equals("3")){
               Vector<String> s=new Vector<>();
               s.add(course.get(i).getId());
               s.add(course.get(i).getName());
               s.add(course.get(i).getTeacher());
               s.add(course.get(i).getCreditHour());
               s.add(course.get(i).getPeriod());
               s.add(course.get(i).getClassTime());
               s.add(course.get(i).getClassRoom());
               s.add(course.get(i).getIntro());
               ss.add(s);
           }
        }

        courses.clear();
        for (Vector<String> sv  : ss) {
            courses.add(sv);
        }
    }



    public Vector<ParseStudent> requestGetData(String url) {
        String result = HttpUtil.sendGet(url);
        Vector<ParseStudent> student = ResultUtil.ResultVectorStudent(result);
        return student;
    }
}
